//
//  GiftListCell.swift
//  MT
//
//  Created by 陈悦 on 2020/5/8.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

class GiftListCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    var isSend: Bool = false {
        didSet {
            moneyLabel.isHidden = isSend
        }
    }
    
    var model: MTGiftReportSubModel? {
        didSet {
            if model != nil {
                headIcon.kf.setImage(with: URL(string: model?.headerUrl ?? ""))
                level.text = "M\(model?.levelId ?? 0)"
                detailLabel.text = isSend ? "送给\(model?.nickname ?? "")":"收到\(model?.nickname ?? "")的礼物"
                timeLab.text = "\(model?.createTime ?? "")"
                countLab.text = "x \(model?.num ?? 0)"
                moneyLabel.text = "+\(model?.price ?? 00)钻"
            }
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    
    lazy var detailLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCMediumFont(12)
        return l
    }()
    
    lazy var timeLab: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor(hex:0x9b9b9b)
        lab.font = PingFangSCMediumFont(10)
        return lab
    }()
    
    lazy var headIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 22
        imageView.backgroundColor = .red
        imageView.clipsToBounds = true
        return imageView
    }()
    
    lazy var level: UILabel = {
        let level = UILabel()
        level.textAlignment = .center
        level.backgroundColor = UIColor(hex:0xffc000)
        level.textColor = .white
        level.font = PingFangSCSemiboldFont(10)
        level.layer.cornerRadius = 6
        level.clipsToBounds = true
        return level
    }()
    
    lazy var moneyLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCMediumFont(12)
        l.textAlignment = .right
        return l
    }()
    
    lazy var rightIcon: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "present")
        return imageView
    }()
    
    lazy var countLab: UILabel = {
        let l = UILabel()
        l.textColor = UIColor(hex:0x1d1a1a)
        l.font = PingFangSCSemiboldFont(16)
        l.textAlignment = .right
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex:0xd8d8d8)
        return v
    }()
    
    
    class func reuseIdentity() -> String {
        return "GiftListCellID"
    }
    
    func bind(model: MTLuckyReportSubModel) {
            
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        makeConstraint()
        contentView.backgroundColor = UIColor.theme.background
        backgroundColor = UIColor.theme.background
    }
    
    private func setupUI() {
        contentView.addSubview(detailLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(moneyLabel)
        contentView.addSubview(headIcon)
        contentView.addSubview(rightIcon)
        contentView.addSubview(timeLab)
        contentView.addSubview(level)
        contentView.addSubview(countLab)
    }
    
    private func makeConstraint() {
        
        headIcon.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(4)
            make.left.equalTo(contentView).offset(15)
            make.width.height.equalTo(44)
        }
        
        level.snp.makeConstraints { (make) in
            make.top.equalTo(headIcon.snp.bottom)
            make.centerX.equalTo(headIcon)
            make.width.equalTo(28)
            make.height.equalTo(12)
        }
        
        detailLabel.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(16)
            make.left.equalTo(headIcon.snp.right).offset(8)
            make.width.equalTo(300)
        }
        
        timeLab.snp.makeConstraints { (make) in
            make.top.equalTo(detailLabel.snp.bottom).offset(4)
            make.left.equalTo(detailLabel)
            make.width.equalTo(300)
        }
        
        countLab.snp.makeConstraints { (make) in
            make.right.equalTo(contentView).offset(-20)
            make.top.equalTo(contentView).offset(13)
            
        }
        
        rightIcon.snp.makeConstraints { (make) in
            make.right.equalTo(countLab.snp.left).offset(-2)
            make.top.equalTo(contentView).offset(12)
            make.width.height.equalTo(22)
        }
        
        moneyLabel.snp.makeConstraints { (make) in
            make.right.equalTo(contentView).offset(-20)
            make.top.equalTo(countLab.snp.bottom).offset(4)
        }
    

        lineView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}

extension GiftListCell {
    
    /// 设置字体大小
    public func mc_addFont(text: String,_ font: UIFont, on range: NSRange) -> NSMutableAttributedString {
        let markAttribute = NSMutableAttributedString(string: text)
        
        if markAttribute.length < range.location + range.length { return markAttribute }
        
        let attrs = [NSAttributedString.Key.font: font]
        markAttribute.addAttributes(attrs, range: range)
        return markAttribute
    }
    
    
}


